package com.jzl.swing.ui;

import java.awt.EventQueue;
import java.awt.FileDialog;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.table.TableModel;

import com.jzl.swing.dao.SupplierDao;
import com.jzl.swing.dao.impl.ReportDaoImpl;
import com.jzl.swing.dao.impl.SupplierDaoImpl;
import com.jzl.swing.entity.Bill;
import com.jzl.swing.entity.BillReport;
import com.jzl.swing.entity.Supplier;
import com.jzl.swing.entity.SupplierReport;
import com.jzl.swing.entity.User;
import com.jzl.swing.object.jactory.ObjectFactory;
import com.jzl.swing.service.BillService;
import com.jzl.swing.service.SupplierService;
import com.jzl.swing.service.UserService;
import com.jzl.swing.service.impl.BillReportServiceImpl;
import com.jzl.swing.service.impl.SupplierReportServerImpl;
import com.jzl.swing.service.impl.SupplierServiceImpl;
import com.jzl.swing.ui.model.ZaleTableModel;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

import javax.swing.JComboBox;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.awt.event.ActionEvent;
import java.awt.CardLayout;
import java.awt.Font;

import javax.swing.JScrollPane;
import javax.swing.JRadioButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MainUI {
	
	/**导出JTable到excel */
    public void exportTable(JTable table, File file) throws IOException {
        TableModel model = table.getModel();
        BufferedWriter bWriter = new BufferedWriter(new FileWriter(file));  
        for(int i=0; i < model.getColumnCount(); i++) {
            bWriter.write(model.getColumnName(i));
            bWriter.write("\t");
        }
        bWriter.newLine();
        for(int i=0; i< model.getRowCount(); i++) {
            for(int j=0; j < model.getColumnCount(); j++) {
                bWriter.write(model.getValueAt(i,j).toString());
                bWriter.write("\t");
            }
            bWriter.newLine();
        }
        bWriter.close();
        System.out.println("write out to: " + file);
    }
    
    //从xls文件中导入数据库
    public void importFileToSupplier(File file) {
    	int i;
		Sheet sheet;
		Workbook book;
		Cell cell1, cell2, cell3, cell4, cell5, cell6;
		try {
			// hello.xls为要读取的excel文件名
			book = Workbook.getWorkbook(file);

			// 获得第一个工作表对象(ecxel中sheet的编号从0开始,0,1,2,3,....)
			sheet = book.getSheet(0);
			// 获取左上角的单元格
			cell1 = sheet.getCell(0, 0);
			System.out.println("标题：" + cell1.getContents());

			i = 1;
			while (true) {
				if (i == 1) {
					i++;
					continue;
				}
				// 获取每一行的单元格
				cell1 = sheet.getCell(0, i);// （列，行）
				cell2 = sheet.getCell(1, i);
				cell3 = sheet.getCell(2, i);
				cell4 = sheet.getCell(3, i);
				cell5 = sheet.getCell(4, i);
				cell6 = sheet.getCell(5, i);
				if ("".equals(cell1.getContents()) == true) // 如果读取的数据为空
					break;

				System.out.println(cell1.getContents() + "\t" + cell2.getContents() + "\t" + cell3.getContents() + "\t"
						+ cell4.getContents() + "\t" + cell5.getContents() + "\t" + cell6.getContents());
				
				String supplier_id = cell1.getContents();	// 供应商编号
				String supplier_name = cell2.getContents();	// 供应商名称
				String supplier_disc = cell3.getContents();
				String supplier_contacter = cell4.getContents();
				String supplier_phone = cell5.getContents();
				String supplier_address = cell6.getContents();
				
				Supplier supplier = new Supplier();
				supplier.setSupplier_id(supplier_id);
				supplier.setSupplier_name(supplier_name);
				supplier.setsupplier_describ(supplier_disc);
				supplier.setsupplier_contacter(supplier_contacter);
				supplier.setsupplier_phone(supplier_phone);
				supplier.setsupplier_address(supplier_address);
				SupplierDao supplierDao = new SupplierDaoImpl();
				if (null==supplierDao.queryById(supplier_id)) {
					new SupplierDaoImpl().add(supplier);
				}
				i++;
			}
			book.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
    }

	public JFrame getFrame() {
		return frame;
	}

	public void setFrame(JFrame frame) {
		this.frame = frame;
	}


	private JFrame frame;
	private User user;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainUI window = new MainUI(args[0], args[1], args[2]);
//					MainUI window = new MainUI();
					
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MainUI() {
		initialize(new User());
	}
	
	public MainUI(String user_name, String user_pass, String user_iden) {
		user = new User(user_name, user_pass, user_iden);
		initialize(user);
	}

	
	private JPanel jPanelMain;
	private JButton btnbillManage;
	private JButton btnSupplyManage;
	private JButton btnUserManage;
	private JButton btnReportManage;
	private JButton btnExit;
	private JTextField textFieldGoodsName;
	private JPanel panelWelcome;
	private JPanel panelManage;
	private JPanel panelSupplierManage;
	private JPanel panelUserManage;
	private JPanel panelReportManage;
	private JTable tableUser;
	private JTable tableBill;
	private JTable tableSupplier;
	private JTable tableBillReport;
	private JTable tableSupplierReport;
	private JScrollPane scrollPane;
	private JScrollPane scrollPaneBillReport;
	private JScrollPane scrollPaneSupplierReport;
	private JPanel panel_Reports;
	private CardLayout cardLayout;
	
	private BillService billService = (BillService) ObjectFactory.getInstance("BillService");
	private UserService userService = (UserService) ObjectFactory.getInstance("UserService");
	private SupplierService supplierService = (SupplierService) ObjectFactory.getInstance("SupplierService");
	ArrayList<String> titleList = new ArrayList<String>();
	private JTextField textFieldUserName;
	private JTextField textFieldSupplierName;
	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize(User userUp) {
		frame = new JFrame();
		frame.setBounds(100, 100, 1020, 600);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		jPanelMain = new JPanel();
		jPanelMain.setLayout(null);
		jPanelMain.setBounds(0,0,1020,600);
		CardLayout cl_panelManage = new CardLayout(0, 0);
		
		
		 
		
		
		
		
		ImageIcon ii = new ImageIcon("images/MainFrame.png");
        int width = ii.getIconWidth();
        int height = ii.getIconHeight();
		
		
		frame.getContentPane().add(jPanelMain);
		
		ImageIcon icon = new ImageIcon("images/btn_bill.gif");
		btnbillManage = new JButton(icon);
		btnbillManage.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				titleList = new ArrayList<String>();
				titleList.add("账单编号");
				titleList.add("商品名称");
				titleList.add("商品数量");
				titleList.add("交易金额");
				titleList.add("是否付款");
				titleList.add("供应商名称");
				titleList.add("商品描述");
				titleList.add("账单时间");
				
				try {
					tableBill.setModel(new ZaleTableModel(titleList, billService.queryAll()));
					System.out.println("110");
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
				cl_panelManage.show(panelManage, "name_281822480602563");
			}
		});
		btnbillManage.setText("账单管理");
		btnbillManage.setBounds(18, 163, 172, 47);
		jPanelMain.add(btnbillManage);
		
		icon = new ImageIcon("images/btn_suppliers.gif");
		btnSupplyManage = new JButton("供应商管理");
		btnSupplyManage.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cl_panelManage.show(panelManage, "name_286591956172497");
				
			}
		});
		btnSupplyManage.setIcon(icon);
		btnSupplyManage.setBounds(18, 222, 172, 47);
		jPanelMain.add(btnSupplyManage);
		if (!userUp.getUser_iden().equals("管理员")) {
			btnSupplyManage.setVisible(false);
		}
		
		icon = new ImageIcon("images/btn_users.gif");
		btnUserManage = new JButton("用户管理");
		btnUserManage.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				titleList = new ArrayList<String>();
				titleList.add("用户名");
				titleList.add("权限");
				try {
					tableUser.setModel(new ZaleTableModel(titleList, userService.queryUserAll(), 1));
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				cl_panelManage.show(panelManage, "name_285908568446744");
			}
		});
		btnUserManage.setIcon(icon);
		btnUserManage.setBounds(18, 285, 172, 47);
		jPanelMain.add(btnUserManage);
		if (!userUp.getUser_iden().equals("管理员")) {
			btnUserManage.setVisible(false);
		}
		
		icon = new ImageIcon("images/btn_reports.gif");
		btnReportManage = new JButton("报表管理");
		btnReportManage.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cl_panelManage.show(panelManage, "name_285924332893670");
				
			}
		});
		btnReportManage.setIcon(icon);
		btnReportManage.setBounds(18, 343, 172, 47);
		jPanelMain.add(btnReportManage);
		
		icon = new ImageIcon("images/btn_exit.gif");
		btnExit = new JButton("退出系统");
		btnExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int i = JOptionPane.showConfirmDialog(null, "是否退出");
				if (i==0) {
					System.exit(0);					
				}
			}
		});
		btnExit.setIcon(icon);
		btnExit.setBounds(18, 412, 172, 47);
		jPanelMain.add(btnExit);
		
		panelManage = new JPanel();
		panelManage.setBounds(202, 135, 812, 459);
		jPanelMain.add(panelManage);
		
		panelManage.setLayout(cl_panelManage);
		
		panelWelcome = new JPanel();
		panelManage.add(panelWelcome, "name_286583873715383");
		panelWelcome.setLayout(null);
		
		JLabel label_2 = new JLabel("欢迎使用超市管理系统！");
		label_2.setFont(new Font("Lucida Grande", Font.PLAIN, 33));
		label_2.setBounds(252, 172, 381, 124);
		panelWelcome.add(label_2);
		
		JPanel panelBillManage = new JPanel();
		panelBillManage.setBackground(Color.LIGHT_GRAY);
		panelManage.add(panelBillManage, "name_281822480602563");
		panelBillManage.setLayout(null);
		
		JPanel panelBlue = new JPanel();
		panelBlue.setBackground(Color.CYAN);
		panelBlue.setBounds(0, 0, 825, 46);
		panelBillManage.add(panelBlue);
		panelBlue.setLayout(null);
		
		JButton button_9 = new JButton("刷新");
		button_9.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ArrayList<Bill> temp = new ArrayList<Bill>();
				try {
					temp = (ArrayList<Bill>) billService.queryAll();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				tableBill.setModel(new ZaleTableModel(titleList, temp));
				return;
			}
		});
		button_9.setBounds(655, 11, 117, 29);
		panelBlue.add(button_9);
		
		JButton btnQuery = new JButton("组合查询");
		btnQuery.setBounds(501, 11, 117, 29);
		panelBlue.add(btnQuery);
		
		JComboBox<String> comboBox = new JComboBox<String>();
		comboBox.setBounds(338, 12, 95, 27);
		panelBlue.add(comboBox);
		comboBox.addItem("请选择");
		comboBox.addItem("是");
		comboBox.addItem("否");
		comboBox.setSelectedIndex(0);
		
		JLabel label_1 = new JLabel("是否付款");
		label_1.setBounds(265, 16, 61, 16);
		panelBlue.add(label_1);
		
		textFieldGoodsName = new JTextField();
		textFieldGoodsName.setBounds(94, 11, 130, 26);
		panelBlue.add(textFieldGoodsName);
		textFieldGoodsName.setColumns(10);
		
		JLabel label = new JLabel("商品名称：");
		label.setBounds(25, 16, 83, 16);
		panelBlue.add(label);
		btnQuery.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String isPaid = comboBox.getSelectedItem().toString();
//				int row = table.getSelectedRow();
//				int column = table.getSelectedColumn();
				ArrayList<Bill> temp = new ArrayList<Bill>();
				if (textFieldGoodsName.getText().equals("")) {
					try {
						temp = (ArrayList<Bill>) billService.queryAll();
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					tableBill.setModel(new ZaleTableModel(titleList, temp));
					return;
				}
				try {
					temp =  (ArrayList<Bill>) billService.queryByNameAndPaid(textFieldGoodsName.getText(), isPaid);
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
				tableBill.setModel(new ZaleTableModel(titleList, temp));
//				System.out.println(table.getSelectedColumn()+":"+table.getSelectedRow());
				
				
			}
		});
		
		
		titleList.add("账单编号");
		titleList.add("商品名称");
		titleList.add("商品数量");
		titleList.add("交易金额");
		titleList.add("是否付款");
		titleList.add("供应商名称");
		titleList.add("商品描述");
		titleList.add("账单时间");
		try {
			tableBill = new JTable(new ZaleTableModel(titleList, billService.queryAll()));
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		
		scrollPane = new JScrollPane(tableBill);
		scrollPane.setBounds(0, 169, 825, 301);
		panelBillManage.add(scrollPane);
		
		JLabel label_6 = new JLabel("账单管理>>");
		label_6.setFont(new Font("Lucida Grande", Font.PLAIN, 20));
		label_6.setBounds(6, 91, 130, 40);
		panelBillManage.add(label_6);
		
		JButton btnOutput = new JButton("导出");
		btnOutput.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				 FileDialog fd = new FileDialog(frame, "保存流水记录", FileDialog.SAVE);
				 fd.setLocation(400, 250);
			     fd.setVisible(true);  
			     String stringfile = fd.getDirectory()+fd.getFile()+".xls";
		         try {
		        	 	exportTable(tableBill, new File(stringfile));
		         } catch (IOException ex) {
		             System.out.println(ex.getMessage());
		             ex.printStackTrace();
		         }

			}
		});
		btnOutput.setBounds(127, 101, 117, 29);
		panelBillManage.add(btnOutput);
		
		JButton btnAddBill = new JButton("添加数据");
		btnAddBill.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new AddBillUI();
				AddBillUI.main(null);
			}
		});
		btnAddBill.setBounds(317, 101, 117, 29);
		panelBillManage.add(btnAddBill);
		
		JButton btnModifyBill = new JButton("修改数据");
		btnModifyBill.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
//				System.out.println(table.getSelectedColumn()+":"+table.getSelectedRow());
				int[] rows = tableBill.getSelectedRows();
				if (rows.length>1) {
					JOptionPane.showMessageDialog(frame, "请勿选中多行数据");
					return;
				}
				int column = tableBill.getSelectedColumn();
				int row = tableBill.getSelectedRow();
				if (column == -1 || row == -1) {
					JOptionPane.showMessageDialog(frame, "请选中一行数据");
					return;
				}
				String string = (String) tableBill.getValueAt(row, 0);
				new ModifyBillUI(string);
				System.out.println((String) tableBill.getValueAt(row, 0));
				String[] strings = {string};
				ModifyBillUI.main(strings);
				
				
			}
		});
		btnModifyBill.setBounds(468, 101, 117, 29);
		panelBillManage.add(btnModifyBill);
		
		JButton btnDeleteBill = new JButton("删除数据");
		btnDeleteBill.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int[] rows = tableBill.getSelectedRows();
				if (rows.length>1) {
//					System.out.println(rows);
//					for (int i : rows) {
//						System.out.println(tableBill.getValueAt(rows[i], 0));
//						
//					}
					
					for(int i = 0; i < rows.length; i++) {
						
						try {
							billService.deleteByNumber((String) tableBill.getValueAt(rows[i], 0));
						} catch (SQLException e1) {
							JOptionPane.showMessageDialog(frame, "部分数据无法正常删除");
							e1.printStackTrace();
						}
					}
					JOptionPane.showMessageDialog(frame, "删除多行数据成功");
					return;
				}
				int column = tableBill.getSelectedColumn();
				int row = tableBill.getSelectedRow();
				if (column == -1 || row == -1) {
					JOptionPane.showMessageDialog(frame, "请选中一行数据");
					return;
				}
				try {
					billService.deleteByNumber((String)tableBill.getValueAt(row, 0));
					JOptionPane.showMessageDialog(frame, "删除成功");
//					frame.dispose();
					
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
		});
		btnDeleteBill.setBounds(641, 101, 117, 29);
		panelBillManage.add(btnDeleteBill);
		
		
		panelSupplierManage = new JPanel();
		panelManage.add(panelSupplierManage, "name_286591956172497");
		panelSupplierManage.setLayout(null);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBackground(Color.CYAN);
		panel_1.setBounds(6, 6, 800, 76);
		panelSupplierManage.add(panel_1);
		panel_1.setLayout(null);
		
		JLabel label_3 = new JLabel("供应商名称：");
		label_3.setBounds(33, 28, 78, 16);
		panel_1.add(label_3);
		
		textFieldSupplierName = new JTextField();
		textFieldSupplierName.setBounds(127, 23, 130, 26);
		panel_1.add(textFieldSupplierName);
		textFieldSupplierName.setColumns(10);
		
		JButton button_4 = new JButton("查询");
		button_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String supplier_name = textFieldSupplierName.getText();
				
				titleList = new ArrayList<String>();
				titleList.add("编号");
				titleList.add("供应商名称");
				titleList.add("供应商描述");
				titleList.add("联系人");
				titleList.add("电话");
				titleList.add("地址");
				
				
				List<Supplier> supplierList = new ArrayList<Supplier>();
				if ("".equals(supplier_name)) {
					try {
						supplierList = supplierService.queryAll();
						
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}else {
					try {
						supplierList = supplierService.queryByName(supplier_name);
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
				tableSupplier.setModel(new ZaleTableModel(titleList, supplierList, "supplier"));
				
				
			}
		});
		button_4.setBounds(309, 23, 117, 29);
		panel_1.add(button_4);
		
		JLabel label_7 = new JLabel("供应商管理>>");
		label_7.setFont(new Font("Lucida Grande", Font.PLAIN, 22));
		label_7.setBounds(39, 120, 149, 50);
		panelSupplierManage.add(label_7);
		
		JButton button_5 = new JButton("导入");
		button_5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					FileDialog fd = new FileDialog(frame, "导入流水记录", FileDialog.LOAD);
					 fd.setLocation(400, 250);
				     fd.setVisible(true);  
				     String stringfile = fd.getDirectory()+fd.getFile();
				     importFileToSupplier(new File(stringfile));
				     JOptionPane.showMessageDialog(frame, "导入文件成功");
				} catch (Exception e2) {
					JOptionPane.showMessageDialog(frame, "导入文件失败");
				}
				
			}
		});
		button_5.setBounds(236, 136, 117, 29);
		panelSupplierManage.add(button_5);
		
		JButton button_6 = new JButton("添加数据");
		button_6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new AddSupplierUI();
				AddSupplierUI.main(null);
				
			}
		});
		button_6.setBounds(389, 136, 117, 29);
		panelSupplierManage.add(button_6);
		
		JButton button_7 = new JButton("修改数据");
		button_7.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int[] rows = tableSupplier.getSelectedRows();
				if (rows.length>1) {
					JOptionPane.showMessageDialog(frame, "请勿选中多行数据");
					return;
				}
				int column = tableSupplier.getSelectedColumn();
				int row = tableSupplier.getSelectedRow();
				if (column == -1 || row == -1) {
					JOptionPane.showMessageDialog(frame, "请选中一行数据");
					return;
				}
				String string = (String) tableSupplier.getValueAt(row, 0);
				System.out.println((String) tableSupplier.getValueAt(row, 0));
				new ModifySupplierUI(string);
				String[] strings = {string};
				ModifySupplierUI.main(strings);
			}
		});
		button_7.setBounds(531, 136, 117, 29);
		panelSupplierManage.add(button_7);
		
		JButton button_8 = new JButton("删除数据");
		button_8.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				

				
				Supplier supplier = new Supplier();
				int[] rows = tableSupplier.getSelectedRows();
				if (rows.length>1) {
//					System.out.println(rows);
//					for (int i : rows) {
//						System.out.println(tableBill.getValueAt(rows[i], 0));
//						
//					}
					
					for(int i = 0; i < rows.length; i++) {
						
						try {
							String supplier_id = (String) tableSupplier.getValueAt(rows[i], 0);
							supplier.setSupplier_id(supplier_id);
							supplierService.deleteSupplier(supplier);
						} catch (SQLException e1) {
							JOptionPane.showMessageDialog(frame, "部分数据无法正常删除");
							e1.printStackTrace();
						} catch (Exception e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
					JOptionPane.showMessageDialog(frame, "删除多行数据成功");
					return;
				}
				int column = tableSupplier.getSelectedColumn();
				int row = tableSupplier.getSelectedRow();
				if (column == -1 || row == -1) {
					JOptionPane.showMessageDialog(frame, "请选中一行数据");
					return;
				}
				
				supplier.setSupplier_id((String)tableSupplier.getValueAt(row, 0));
				try {
					int i = supplierService.deleteSupplier(supplier);
					if (i>0) {
						JOptionPane.showMessageDialog(frame, "删除成功！");
//						frame.dispose();
					}
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});
		button_8.setBounds(660, 136, 117, 29);
		panelSupplierManage.add(button_8);
		
		titleList = new ArrayList<String>();
		titleList.add("编号");
		titleList.add("供应商名称");
		titleList.add("供应商描述");
		titleList.add("联系人");
		titleList.add("电话");
		titleList.add("地址");
		try {
			tableSupplier = new JTable(new ZaleTableModel(titleList, supplierService.queryAll(), "supplier"));
		} catch (Exception e3) {
			// TODO Auto-generated catch block
			e3.printStackTrace();
		}
		JScrollPane scrollPaneSuppluer = new JScrollPane(tableSupplier);
		scrollPaneSuppluer.setBounds(6, 178, 800, 263);
		panelSupplierManage.add(scrollPaneSuppluer);
		
		panelUserManage = new JPanel();
		panelManage.add(panelUserManage, "name_285908568446744");
		panelUserManage.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBackground(Color.CYAN);
		panel.setForeground(Color.BLACK);
		panel.setBounds(6, 6, 800, 106);
		panelUserManage.add(panel);
		panel.setLayout(null);
		
		JLabel labelUserName = new JLabel("用户名称：");
		labelUserName.setBounds(21, 27, 81, 16);
		panel.add(labelUserName);
		
		textFieldUserName = new JTextField();
		textFieldUserName.setBounds(119, 22, 130, 26);
		panel.add(textFieldUserName);
		textFieldUserName.setColumns(10);
		
		JButton button = new JButton("查询");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String userName = textFieldUserName.getText();
				List<User> userList = new ArrayList<User>();
				if ("".equals(userName)) {
					try {
						userList = userService.queryUserAll();
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}else {
					
					try {
						User user = new User();
						user.setUser_name(textFieldUserName.getText());
						userList.add(userService.queryUser(user));
						System.out.println(userList.get(0).getUser_name());
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					
				}
				tableUser.setModel(new ZaleTableModel(titleList, userList, 1));
				
			}
		});
		button.setBounds(302, 22, 117, 29);
		panel.add(button);
		
		JLabel label_4 = new JLabel("用户管理>>");
		label_4.setFont(new Font("Lucida Grande", Font.PLAIN, 20));
		label_4.setBounds(16, 124, 142, 47);
		panelUserManage.add(label_4);
		
		JButton button_1 = new JButton("添加用户");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new AddUserUI();
				AddUserUI.main(null);
				
			}
		});
		button_1.setBounds(211, 137, 117, 29);
		panelUserManage.add(button_1);
		
		JButton button_2 = new JButton("修改用户");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				int[] rows = tableUser.getSelectedRows();
				if (rows.length>1) {
					JOptionPane.showMessageDialog(frame, "请勿选中多行数据");
					return;
				}
				int column = tableUser.getSelectedColumn();
				int row = tableUser.getSelectedRow();
				if (column == -1 || row == -1) {
					JOptionPane.showMessageDialog(frame, "请选中一行数据");
					return;
				}
				String user_name = (String) tableUser.getValueAt(row, 0);
				try {
					String[] args = {user_name};
					ModifyUserUI.main(args);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				
				
			}
		});
		button_2.setBounds(391, 137, 117, 29);
		panelUserManage.add(button_2);
		
		JButton button_3 = new JButton("删除用户");
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				User user = new User();
				int[] rows = tableUser.getSelectedRows();
				if (rows.length>1) {
//					JOptionPane.showMessageDialog(frame, "请勿选中多行数据");
					for(int i = 0; i < rows.length; i++) {
						user.setUser_name((String)tableUser.getValueAt(rows[i], 0));
						if (user.getUser_name().equals(labelUserName.getText())) {
							
							JOptionPane.showConfirmDialog(frame, "不能删除自己");
							return;
						}
						try {
							userService.deleteUser(user);
						} catch (Exception e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
					JOptionPane.showMessageDialog(frame, "删除多行数据成功");
					return;
				}
				int column = tableUser.getSelectedColumn();
				int row = tableUser.getSelectedRow();
				if (column == -1 || row == -1) {
					JOptionPane.showMessageDialog(frame, "请选中一行数据");
					return;
				}
				try {
					user.setUser_name((String) tableUser.getValueAt(row, 0));
					if (user.getUser_name().equals(labelUserName.getText())) {
						JOptionPane.showConfirmDialog(frame, "不能删除自己");
						return;
					}
					userService.deleteUser(user);
					JOptionPane.showMessageDialog(frame, "删除成功");
				} catch (SQLException e1) {
					e1.printStackTrace();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				
				
			}
		});
		button_3.setBounds(581, 137, 117, 29);
		panelUserManage.add(button_3);
		titleList = new ArrayList<String>();
		titleList.add("用户名");
		titleList.add("权限");
		try {
			tableUser = new JTable(new ZaleTableModel(titleList, userService.queryUserAll(), 1));
		} catch (Exception e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		JScrollPane scrollPaneUser = new JScrollPane(tableUser);
		scrollPaneUser.setBounds(6, 178, 800, 216);
		panelUserManage.add(scrollPaneUser);
		
		panelReportManage = new JPanel();
		panelManage.add(panelReportManage, "name_285924332893670");
		panelReportManage.setLayout(null);
		
		ButtonGroup buttonGroup = new ButtonGroup();
		
		JRadioButton radioButton = new JRadioButton("商品分组查询");
		radioButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
//				JOptionPane.showMessageDialog(frame, "1");
//				String userName = textFieldUserName.getText();
//				List<BillReport> billReports = new ArrayList<BillReport>();
//				billReports = new ReportDaoImpl().queryByProduct();
//				List<String>titleList1 = new ArrayList<String>();
//				titleList1.add("商品名称");
//				titleList1.add("交易数量");
//				titleList1.add("交易金额");
//				titleList1.add("供应商名称");
//				
				System.out.println("yes clicked");
				cardLayout.show(panel_Reports, "name_353913250875111");
				
			}
		});
		radioButton.setBounds(68, 52, 141, 23);
		panelReportManage.add(radioButton);
		
		JRadioButton radioButton_1 = new JRadioButton("供应商分组查询");
		radioButton_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				cardLayout.show(panel_Reports, "scrSupplierReport");
			}
		});
		radioButton_1.setBounds(360, 52, 141, 23);
		panelReportManage.add(radioButton_1);
		buttonGroup.add(radioButton);
		buttonGroup.add(radioButton_1);
		
		panel_Reports = new JPanel();
		cardLayout = new CardLayout(0, 0);
		panel_Reports.setBounds(6, 102, 800, 332);
		panelReportManage.add(panel_Reports);
		panel_Reports.setLayout(cardLayout);
		
		
		List<BillReport> billReports = new ArrayList<BillReport>();
//		billReports = new ReportDaoImpl().queryByProduct();
		billReports = new BillReportServiceImpl().queryAllNoReport();
		titleList = new ArrayList<String>();
		titleList.add("商品名称");
		titleList.add("交易数量");
		titleList.add("交易金额");
		titleList.add("供应商名称");
		tableBillReport = new JTable(new ZaleTableModel(titleList, billReports, true));
		scrollPaneBillReport = new JScrollPane(tableBillReport);
		panel_Reports.add(scrollPaneBillReport, "name_353913250875111");
		
		List<SupplierReport> supplierReports = new ArrayList<SupplierReport>();
//		supplierReports = new ReportDaoImpl().queryBySupplier();
		supplierReports = new SupplierReportServerImpl().queryAllNoRepeat();
		titleList = new ArrayList<String>();
		titleList.add("供应商编号");
		titleList.add("供应商名称");
		titleList.add("交易金额");
		titleList.add("商品数量");
		tableSupplierReport = new JTable(new ZaleTableModel(titleList, supplierReports, true, 1));
		scrollPaneSupplierReport = new JScrollPane(tableSupplierReport);
		panel_Reports.add(scrollPaneSupplierReport, "scrSupplierReport");
		
		
		JLabel label_5 = new JLabel("欢迎您：");
		label_5.setBounds(582, 62, 61, 16);
		jPanelMain.add(label_5);
		
		JLabel labelUserNameMainUserNameMain = new JLabel("");
		labelUserNameMainUserNameMain.setBounds(640, 62, 61, 16);
		jPanelMain.add(labelUserNameMainUserNameMain);
		labelUserNameMainUserNameMain.setText(userUp.getUser_name());
		
		JLabel lblTime = new JLabel("Time");
		lblTime.setBounds(759, 79, 189, 16);
		jPanelMain.add(lblTime);
		
		SimpleDateFormat simpleDateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		lblTime.setText(simpleDateFormat.format(new Date()));
		
		
		
		
		
		BackImage bi = new BackImage(width, height, "images/MainFrame.png");
		bi.setBounds(0, 0, width, height);
		jPanelMain.add(bi);
		
		
		
		
	}
}
